$.ajax({
    url: "/authenticate_",
    type: "GET",
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function (response) {
        if (response.code === 200) {
            console.log(response);
            $(".login").hide();
            $(".signin").hide();
            $(".publish").show();
            $(".user").show();
            $(".avatar_url").attr("src", response.data.avatar_url);
            $(".username").text(response.data.username);
        }
    },
    error: function (request) {
        console.log(request);
    }
});
$("#avatarImage").change(function () {
    var chooseFile = $(this)[0].files[0];
    var type = chooseFile.name.substring(chooseFile.name.lastIndexOf(".") + 1);
    type = type.toLowerCase();
    if (type == "jpg" || type == "png" || type == "JPG" || type == "jepg" || type == "gif" || type == "bmp" || type == "webp") {
        var reader = new FileReader();
        reader.readAsDataURL(chooseFile);
        reader.onload = function () {
            $(".avatar_url").attr("src", this.result);
            $("#submitImage").click();
        };
    } else {
        alert("头像格式不对，请重新选择！");
        return false;
    }
});

function upload() {
    $("#avatarImage").click();
}

$(".index").click(function () {
    window.location.href = "http://localhost/personal.html";
});

$(".questions").click(function () {
    getPersonalQuestionsByPage(1);
});

function getPersonalQuestionsByPage(page) {
    $.ajax({
        url: "/personal/questions/" + page,
        type: "GET",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        success: function (response) {
            console.log(response);
            $(".wrap").empty();
            for (var i = 0; i < response.data.length; i++) {
                var info = "<a style='margin:10px 0px;overflow: hidden;text-overflow: ellipsis;height: 60px;' href=\"" +
                    "/question/" + response.data[i].id +
                    "\" class=\"list-group-item text-nowrap\">\n" +
                    "                    <h4 class=\"list-group-item-heading\">" +
                    response.data[i].title +
                    "</h4>\n" +
                    "                    <p class=\"list-group-item-text\"></p>\n" +
                    "                    <p>\n" +
                    "                        <span class=\"pull-right glyphicon glyphicon-time\" >" +
                    response.data[i].modifiedAt +
                    "</span>\n" +
                    "                        <span class=\"pull-right glyphicon glyphicon-thumbs-up\">" +
                    response.data[i].praiseCount +
                    "</span>\n" +
                    "                        <span class=\"pull-right glyphicon glyphicon-eye-open\">" +
                    response.data[i].readCount +
                    "</span>\n" +
                    "                        <span class=\"pull-right glyphicon glyphicon-comment\" >" +
                    response.data[i].commentCount +
                    "</span>\n" +
                    "                    </p>\n" +
                    "                </a>";
                $(".wrap").append(info);
            }
            getPagination("questions", page);
        },
        error: function (request) {
            console.log(request);
        }
    });
}

$(".replies").click(function () {
    getPersonalRepliesByPage(1);
});

function getPagination(mark, currentPage) {
    $.ajax({
        type: "GET",
        url: "/personal/" + mark + "/page/" + currentPage,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var firstPage = "";
            var lastPage = "";
            var method = null;
            if (mark == "questions") {
                method = "getPersonalQuestionsByPage";
                firstPage += "getPersonalQuestionsByPage(1)";
                lastPage += "getPersonalQuestionsByPage(" + response.data.lastPage + ")";
            } else if (mark == "replies") {
                method = "getPersonalRepliesByPage";
                firstPage += "getPersonalRepliesByPage(1)";
                lastPage += "getPersonalRepliesByPage(" + response.data.lastPage + ")";
            }
            $("#pagination").empty();
            var info_part_1 = "<li>\n" +
                "                        <a href='#' onclick='" +
                firstPage +
                "' aria-label=\"Previous\">\n" +
                "                            <span aria-hidden=\"true\">&laquo;</span>\n" +
                "                        </a>\n" +
                "                    </li> ";
            $("#pagination").append(info_part_1);
            for (var i = 1; i <= response.data.pages.length; i++) {
                var info_part_2 = "<li class='" +
                    "page-" + response.data.pages[i - 1] +
                    "'><a  " +
                    "href=" +
                    "#" +
                    " onclick='" + method + "(" + response.data.pages[i - 1] +
                    ")'" +
                    ">" +
                    response.data.pages[i - 1] +
                    "</li>";
                $("#pagination").append(info_part_2)
            }
            var info_part_3 = "<li>\n" +
                "                        <a  href=\"#\" aria-label=\"Next\" onclick='" +
                lastPage +
                "'>\n" +
                "                            <span aria-hidden=\"true\">&raquo;</span>\n" +
                "                        </a>\n" +
                "              </li>";
            $("#pagination").append(info_part_3);
            $(".page-" + currentPage).addClass("active");
        },
        error: function (request) {
            console.log(request);
        }
    });
}

function getPersonalRepliesByPage(page) {
    $.ajax({
        url: "/personal/replies/" + page,
        dataType: "json",
        type: "GET",
        contentType: "application/json;charset=utf-8",
        success: function (response) {
            $(".wrap").empty();
            console.log(response);
            for (var i = 0; i < response.data.length; i++) {
                var bonus = response.data[i].comment.type === 1 ? "    回复了问题：" : "    回复了评论：";
                var bonus_ = "";
                var url = "";
                if (response.data[i].comment.type === 1) {
                    url += "href='/question/" + response.data[i].commentedQuestionOrComment.id + "'";
                    bonus_ += response.data[i].commentedQuestionOrComment.title;
                } else {
                    bonus_ += response.data[i].commentedQuestionOrComment.content;
                }
                var unreadOrRead = "";
                if (response.data[i].status !== 1) {
                    unreadOrRead += "<span class='label label-danger'><span class='glyphicon glyphicon-tag'>未读</span></span>";
                }
                var info = "<div class=\"list-group\">\n" +
                    "  <a " +
                    url +
                    " class=\"list-group-item\" onclick='readIt(" +
                    response.data[i].id +
                    ")'>\n" +
                    "    <h4 class=\"list-group-item-heading\">" +
                    "<strong class='text-primary'>" +
                    response.data[i].comment.user.username +
                    "</strong>" +
                    "<span>" + bonus + "</span>" +
                    "<span>" + bonus_ + "</span>" +
                    "</h4>\n" +
                    "<p class=\"list-group-item-text\">" +
                    response.data[i].comment.content +
                    unreadOrRead +
                    "</p>\n" +
                    "<p>" +
                    "   <span class='pull-right label label-info'>" +
                    "       <span class='glyphicon-time glyphicon'>" +
                    response.data[i].comment.createdAt +
                    "       </span>" +
                    "   </span>" +
                    "</p>" +
                    "  </a>\n" +
                    "</div>";
                $(".wrap").append(info);
            }
            // $("#pagination").remove();
            getPagination("replies",page);
        },
        error: function (request) {
            console.log(request);
        }
    });
}